package com.ruoyi.zyt.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.zyt.domain.Colleges;
import com.ruoyi.zyt.domain.EvaluateDTO;
import com.ruoyi.zyt.domain.User;
import com.ruoyi.zyt.mapper.CollegesMapper;
import com.ruoyi.zyt.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zyt.mapper.AspirationMapper;
import com.ruoyi.zyt.domain.Aspiration;
import com.ruoyi.zyt.service.IAspirationService;

/**
 * 志愿填报Service业务层处理
 *
 * @author zyt
 * @date 2023-11-05
 */
@Service
public class AspirationServiceImpl implements IAspirationService {
    @Autowired
    private AspirationMapper aspirationMapper;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private CollegesMapper collegesMapper;

    /**
     * 查询志愿填报
     *
     * @param id 志愿填报主键
     * @return 志愿填报
     */
    @Override
    public Aspiration selectAspirationById(Long id) {
        return aspirationMapper.selectAspirationById(id);
    }

    /**
     * 查询志愿填报列表
     *
     * @param aspiration 志愿填报
     * @return 志愿填报
     */
    @Override
    public List<Aspiration> selectAspirationList(Aspiration aspiration) {
        return aspirationMapper.selectAspirationList(aspiration);
    }

    /**
     * 新增志愿填报
     *
     * @param aspiration 志愿填报
     * @return 结果
     */
    @Override
    public int insertAspiration(Aspiration aspiration) {
        return aspirationMapper.insertAspiration(aspiration);
    }

    /**
     * 修改志愿填报
     *
     * @param aspiration 志愿填报
     * @return 结果
     */
    @Override
    public int updateAspiration(Aspiration aspiration) {
        return aspirationMapper.updateAspiration(aspiration);
    }

    /**
     * 批量删除志愿填报
     *
     * @param ids 需要删除的志愿填报主键
     * @return 结果
     */
    @Override
    public int deleteAspirationByIds(Long[] ids) {
        return aspirationMapper.deleteAspirationByIds(ids);
    }

    /**
     * 删除志愿填报信息
     *
     * @param id 志愿填报主键
     * @return 结果
     */
    @Override
    public int deleteAspirationById(Long id) {
        return aspirationMapper.deleteAspirationById(id);
    }


    @Override
    public EvaluateDTO evaluate(String userId) {
        Aspiration aspiration = new Aspiration();
        aspiration.setUserId(userId);
        List<Aspiration> aspirations = aspirationMapper.selectAspirationList(aspiration);
//        HashSet<String> set = new HashSet<>();
        if (aspirations.size() == 0) {
            return new EvaluateDTO();
        }
        ArrayList<String> set = new ArrayList<>();
        for (Aspiration aspiration1 : aspirations) {
            set.add(aspiration1.getCollage());
        }
        List<Colleges> colleges = collegesMapper.getByName(set);
        Map<String, List<Aspiration>> collect = aspirations.stream().collect(Collectors.groupingBy(Aspiration::getType));
        EvaluateDTO evaluateDTO = new EvaluateDTO();
        evaluateDTO.setCollegeNum(collect.get("本科")==null?0:collect.get("本科").size());
        evaluateDTO.setSpecialNum(collect.get("专科")==null?0:collect.get("专科").size());
        User user = new User();
        user.setId(Long.valueOf(userId));
        List<User> users = iUserService.selectUserList(user);
        User user1 = users.get(0);
        Long num = user1.getNum();

        Integer sprint = 0;
        Integer stable = 0;
        Integer guaranteed = 0;
        Integer must = 0;
        for (Colleges college : colleges) {
            int score = college.getNum2023() - num.intValue();
            if (score >= 20) {
                sprint++;
                continue;
            }
            if (score >= 0 && score < 20) {
                stable++;
                continue;
            }
            if (score >= -10 && score < 0) {
                guaranteed++;
                continue;
            }
            if (score < -10) {
                must++;

            }
        }
        evaluateDTO.setStable(stable);
        evaluateDTO.setSprint(sprint);
        evaluateDTO.setGuaranteed(guaranteed);
        evaluateDTO.setMust(must);
        return evaluateDTO;
    }
}
